package com.zhongkun.datahome.service.domain;

import com.zhongkun.datahome.service.constant.EntityTypeEnum;
import com.zhongkun.datahome.service.constant.FileTypeEnum;
import com.zhongkun.datahome.service.constant.YesOrNoEnum;
import com.zhongkun.datahome.service.req.cmd.assets.DataAssetsCmd;
import com.zhongkun.datahome.service.req.cmd.file.CommonFileCmd;
import com.zhongkun.datahome.service.req.cmd.file.SignFileCmd;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用文件对象 common_file
 *
 * @author ruoyi
 * @date 2025-04-14
 */
@Data
public class CommonFile extends BaseEntity {
    /**
     * @see com.zhongkun.datahome.service.constant.EntityTypeEnum
     */
    @ApiModelProperty("关联实体类型")
    private String entityType;

    @ApiModelProperty("关联实体标志")
    private String entityId;

    /**
     * @see com.zhongkun.datahome.service.constant.FileTypeEnum
     */
    @ApiModelProperty("文件类型")
    private String fileType;

    @ApiModelProperty("文件url")
    private String fileUrl;

    @ApiModelProperty("文件大小")
    private String fileSize;

    @ApiModelProperty("文件名称")
    private String fileName;

    @ApiModelProperty("排序")
    private Integer sort;

    @ApiModelProperty("是否头图")
    private Integer isHead;

    @ApiModelProperty("文件备注")
    private String remark;

    public static CommonFile convertSkuCommonPic(Long skuId, String url, int sort) {
        CommonFile commonFile = new CommonFile();
        commonFile.setEntityType(EntityTypeEnum.SKU_FILE.getType());
        commonFile.setEntityId(skuId.toString());
        commonFile.setFileUrl(url);
        commonFile.setFileType(FileTypeEnum.COMMON_PIC.getType());
        commonFile.setFileName("商品图片");
        commonFile.setFileSize("");
        commonFile.setSort(sort);
        commonFile.setIsHead(YesOrNoEnum.NO.getCode());

        return commonFile;
    }

    public static CommonFile convertSignFile(Long houseId, SignFileCmd cmd) {
        CommonFile commonFile = new CommonFile();
        commonFile.setEntityType(EntityTypeEnum.SIGN_FILE.getType());
        commonFile.setEntityId(houseId.toString());
        commonFile.update(cmd);

        return commonFile;
    }

    public void update(SignFileCmd cmd) {
        this.setFileUrl(cmd.getFileUrl());
        this.setFileName(cmd.getFileName());
        this.setFileType(FileTypeEnum.getFileType(cmd.getFileUrl()));
        this.setSort(cmd.getSort());
    }

    public static CommonFile convertDataAssetsFile(Long assetsId, String url, String assetsTitle) {
        CommonFile commonFile = new CommonFile();
        commonFile.setEntityType(EntityTypeEnum.ASSETS_FILE.getType());
        commonFile.setEntityId(assetsId.toString());
        commonFile.setFileUrl(url);
        commonFile.setFileName(assetsTitle);
        commonFile.setFileType(FileTypeEnum.getFileType(url));

        return commonFile;
    }

    public static CommonFile convertDataAssetsFile(Long assetsId, DataAssetsCmd.FileEntity file, String assetsTitle) {
        CommonFile commonFile = new CommonFile();
        commonFile.setEntityType(EntityTypeEnum.ASSETS_FILE.getType());
        commonFile.setEntityId(assetsId.toString());
        commonFile.setFileUrl(file.getUrl());
        commonFile.setRemark(file.getRemark());
        commonFile.setFileName(assetsTitle);
        commonFile.setFileType(FileTypeEnum.getFileType(file.getUrl()));

        return commonFile;
    }

    public static List<CommonFile> convertTaskFile(Long entityId, List<CommonFileCmd> commonFileCmdList, EntityTypeEnum entityTypeEnum) {
        List<CommonFile> commonFileList = new ArrayList<>();
        for (int i = 0; i < commonFileCmdList.size(); i++) {
            CommonFile commonFile = new CommonFile();
            commonFile.setEntityType(entityTypeEnum.getType());
            commonFile.setEntityId(String.valueOf(entityId));
            commonFile.setFileUrl(commonFileCmdList.get(i).getFileUrl());
            commonFile.setFileType(FileTypeEnum.getFileType(commonFile.getFileUrl()));
            commonFile.setFileName(commonFileCmdList.get(i).getFileName());
            commonFile.setSort(i + 1);
            commonFileList.add(commonFile);
        }
        return commonFileList;
    }
}
